import threading
import time

import grpc
import rx, rx.operators as ops

from demo import server_stream_pb2_grpc, server_stream_pb2

def thread2(stub):
    req = server_stream_pb2.Query(keyword='java')

    print('Client start streaming...')
    for result in stub.SearchStream(req):
        print(f'→ {result.title} | {result.url}')


def run():
    channel = grpc.insecure_channel('localhost:50051')
    stub = server_stream_pb2_grpc.SearchStub(channel)

    t2 = threading.Thread(target=thread2, args=(stub,), daemon=True)
    #t1.start()
    t2.start()

    try:
        threading.Event().wait()
        while True:
            time.sleep(1)

    except KeyboardInterrupt:
        channel.close()

if __name__ == '__main__':
    run()